package com.woniu.atms.controller.aft;

import com.github.pagehelper.PageInfo;
import com.woniu.atms.entity.G5Site;
import com.woniu.atms.service.SiteService;
import com.woniu.atms.util.Code;
import com.woniu.atms.util.ResponseResult;
import com.woniu.atms.vo.aft.site.SitePageVO;
import com.woniu.atms.vo.aft.site.group.AddGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author lhy
 * @title: AftSiteController 站点管理
 * @projectName g5_atms
 * @date 2022/1/17 15:15
 */
@Slf4j
@RestController
@RequestMapping("/api/aft/v1/site")
public class AftSiteController {
    //站点列表(/list);查询站点名(/list_name);查询站点(/info_id);查询站点(/list_address);添加站点(/add);
    //删除站点(/delete);修改站点信息(/update);查询所有站点名(/siteNames)
    @Autowired
    private SiteService siteService;

    /**
     * 查询站点列表,分页
     * @return
     */
    @RequestMapping("/list")
    public ResponseResult findAllSite(@RequestBody SitePageVO sitePageVO){
        if(sitePageVO.getPageNum()==null){
            sitePageVO.setPageNum(1);
        }
        PageInfo<G5Site> pageInfo =siteService.findAllSite(sitePageVO.getPageNum());
        ResponseResult responseResult=new ResponseResult(pageInfo);
        return responseResult;
    }

    /**
     * 根据id查询站点
     * @param
     * @return
     */
    @PostMapping("/info_id")
    public ResponseResult findSiteById(@RequestBody @Validated G5Site site){
        G5Site site2=siteService.findSiteById(site.getId());
        return new ResponseResult(site2);
    }

    /**
     * 根据站点名模糊查询
     * @return
     */
    @PostMapping("/list_name")
    public ResponseResult findSiteByFuzzyName(@RequestBody G5Site site){
//        log.debug("siteName:{}",site.getName());
        List<G5Site> sites=siteService.findSiteByFuzzyName(site.getName());
        return new ResponseResult(sites);
    }

    /**
     * 根据站点所属地区名模糊查询站点
     * @param site
     * @return
     */
    @PostMapping("/list_address")
    public ResponseResult findSiteByFuzzyAddress(@RequestBody G5Site site){
        log.debug("siteName:{}",site.getAddress());
        List<G5Site> sites= siteService.findSiteByFuzzyAddress(site.getAddress());
        return new ResponseResult(sites);
    }

    /**
     * 添加站点
     * @param site
     * @return
     */
    @PostMapping("/add")
    public ResponseResult addSite(@RequestBody @Validated(AddGroup.class) G5Site site){
        //判断名字是否和已有可用的站点名重复
        String siteName=site.getName();
        List<G5Site> sites1=siteService.findSiteByFuzzyName(siteName);
        for(G5Site s:sites1){
            if(s.getName().equals(siteName)){
                return new ResponseResult(Code.FAIL,"站点名重复",null);
            }
        }
        boolean flag=siteService.addSite(site);
        if(flag){
            return new ResponseResult(Code.SUCCESS,"添加成功！",null);
        }else {
            return new ResponseResult(Code.FAIL,"添加失败！",null);
        }
    }

    /**
     * 删除站点
     * @param site
     * @return
     */
    @PostMapping("/delete")
    public ResponseResult deleteSite(@RequestBody G5Site site){
        log.debug("siteName:{}",site.getId());
        boolean flag=siteService.deleteSiteById(site.getId());
        if(flag){
            return new ResponseResult(Code.SUCCESS,"删除成功！",null);
        }else {
            return new ResponseResult(Code.FAIL,"删除失败！",null);
        }
    }

    /**
     * 更新站点信息
     * @param site
     * @return
     */
    @PostMapping("/update")
    public ResponseResult updateSite(@RequestBody G5Site site){
        boolean flag=siteService.updateSite(site);
        if(flag){
            return new ResponseResult(Code.SUCCESS,"更新成功！",null);
        }else {
            return new ResponseResult(Code.FAIL,"更新失败！",null);
        }
    }

    /**
     * 查询所有站点名
     * @return
     */
    @GetMapping("/siteNames")
    public ResponseResult findALLSiteName(){
        List<String> siteNames=siteService.findAllSiteNames();
        return new ResponseResult(siteNames);
    }
}
